//
//  AVRequest+FC.m
//  项目三
//
//  Created by rimi1 on 2016/12/12.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "AVRequest+FC.h"

@implementation AVRequest (FC)
// 查询粉丝
+ (void)avFansWithObj:(AVObject *)obj objArr:(QueryArr)objArr {
    AVRelation *relation = [obj relationForKey:@"fans"];
    AVQuery *query = [relation query];
    [AVRequest avQueryWithQuery:query queryArr:^(NSArray *objects, NSError *error) {
        if (objArr) {
            objArr(objects, error);
        }
    }];
}
// 查询关注
+ (void)avConcernWithObj:(AVObject *)obj objArr:(QueryArr)objArr {
    AVRelation *relation = [obj relationForKey:@"concern"];
    AVQuery *query = [relation query];
    [AVRequest avQueryWithQuery:query queryArr:^(NSArray *objects, NSError *error) {
        if (objArr) {
            objArr(objects, error);
        }
    }];
}
// 查询是否关注，返回YES，表示已关注该用户
+ (void)avIsConcernWithObjId:(NSString *)objId succeed:(AVSucceed)succeed {
    AVObject *myUser = [AVObject objectWithClassName:@"MyUser" objectId:[MyUser sharedUser].userId];
    AVRelation *relation = [myUser relationForKey:@"concern"];
    AVQuery *query = [relation query];
    [query whereKey:@"objectId" equalTo:objId];
    [AVRequest avQueryWithQuery:query queryArr:^(NSArray *objects, NSError *error) {
        if (error) {
            succeed(NO, error);
            return;
        }
        
        if (objects.count == 0) {
            succeed(NO, error);
        }else {
            succeed(YES, error);
        }
        
    }];
}

+ (void)avRemoveFansWithObj:(AVObject *)obj removeObj:(AVObject *)removeObj saveSucceed:(AVSucceed)saveSucceed {
    // 删除粉丝
    [AVRequest avRFansWithObj:obj removeObj:removeObj saveSucceed:^(BOOL succeed, NSError *error) {
        saveSucceed(succeed, error);
    }];
    // 被删除的，删除关注
    [AVRequest avRConcernWithObj:removeObj removeObj:obj saveSucceed:^(BOOL succeed, NSError *error) {
        
    }];
}

+ (void)avAddConcernWithObj:(AVObject *)obj addObj:(AVObject *)addObj saveSucceed:(AVSucceed)saveSucceed {
    // 添加关注
    [AVRequest avAConcernWithObj:obj addObj:addObj saveSucceed:^(BOOL succeed, NSError *error) {
        saveSucceed(succeed, error);
    }];
    // 被关注的添加粉丝
    [AVRequest avAddFansWithObj:addObj addObj:obj saveSucceed:^(BOOL succeed, NSError *error) {
        
    }];
}

+ (void)avRemoveConcernWithObj:(AVObject *)obj removeObj:(AVObject *)removeObj saveSucceed:(AVSucceed)saveSucceed {
    // 删除关注
    [AVRequest avRConcernWithObj:obj removeObj:removeObj saveSucceed:^(BOOL succeed, NSError *error) {
        saveSucceed(succeed, error);
    }];
    // 被删除关注的，删除粉丝
    [AVRequest avRemoveFansWithObj:removeObj removeObj:obj saveSucceed:^(BOOL succeed, NSError *error) {
        
    }];
}

#pragma mark - BaseAddRemove

// 添加粉丝
+ (void)avAddFansWithObj:(AVObject *)obj addObj:(AVObject *)addObj saveSucceed:(AVSucceed)saveSucceed {
    NSInteger count = [obj[@"fansCount"] integerValue] + 1;
    NSString *countStr = [NSString stringWithFormat:@"%ld", count];
    [obj setObject:countStr forKey:@"fansCount"];
    
    [AVRequest avSaveAllAddWithObj:obj key:@"fans" objArr:@[addObj] aSucceed:^(BOOL succeed, NSError *error) {
        saveSucceed(succeed, error);
    }];
}
// 删除粉丝
+ (void)avRFansWithObj:(AVObject *)obj removeObj:(AVObject *)removeObj saveSucceed:(AVSucceed)saveSucceed {
    NSInteger count = [obj[@"fansCount"] integerValue] - 1;
    NSString *countStr = [NSString stringWithFormat:@"%ld", count];
    [obj setObject:countStr forKey:@"fansCount"];
    
    [AVRequest avSaveAllRemoveWithObj:obj key:@"fans" objArr:@[removeObj] rSucceed:^(BOOL succeed, NSError *error) {
        saveSucceed(succeed, error);
    }];
}
// 添加关注
+ (void)avAConcernWithObj:(AVObject *)obj addObj:(AVObject *)addObj saveSucceed:(AVSucceed)saveSucceed {
    // 添加关注
    [AVRequest avSaveAllAddWithObj:obj key:@"concern" objArr:@[addObj] aSucceed:^(BOOL succeed, NSError *error) {
        saveSucceed(succeed, error);
    }];
}
// 删除关注
+ (void)avRConcernWithObj:(AVObject *)obj removeObj:(AVObject *)removeObj saveSucceed:(AVSucceed)saveSucceed {
    [AVRequest avSaveAllRemoveWithObj:obj key:@"concern" objArr:@[removeObj] rSucceed:^(BOOL succeed, NSError *error) {
        saveSucceed(succeed, error);
    }];
}

@end














